﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PrimeNumbers
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            const uint max = 100000000;

            bool[] flags = new bool[max+1];
            flags[0] = flags[1] = false;
            for (uint i = 2; i < flags.Length; i++)
                flags[i] = true;

            uint prime = 2;
            while (prime < flags.Length)
            {
                for (uint i = prime * prime; i < flags.Length; i += prime)
                {
                    flags[i] = false;
                }

                do
                {
                    prime++;
                } while (prime < flags.Length && !flags[prime]);
            }

            uint count = 0;
            StringBuilder stringBuilder = new StringBuilder();
            for (uint i = 0; i < flags.Length; i++)
            {
                if (flags[i])
                {
                    stringBuilder.Append(i.ToString() + ", ");
                    count++;
                    if (count % 20 == 0)
                        stringBuilder.Append(Environment.NewLine);
                }
            }

            textBox1.Text = stringBuilder.ToString();
            this.Text = count.ToString();
        }
    }
}
